package com.simple.portal.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.portal.bo.IApplicationBO;
import com.simple.portal.dao.IApplicationDAO;
import com.simple.portal.vo.ApplicationVO;
import com.simple.portal.vo.OrganizationVO;
import com.simple.util.common.Constants;
import com.simple.util.common.DateUtil;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.portal.ApplicationBean;
import com.simple.util.object.portal.SessionUserBean;

@Service("applicationBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class ApplicationBOImpl implements IApplicationBO {

	@Autowired
	private IApplicationDAO applicationDAO;
	@Override
	public ApplicationVO findById(String id) {
		return applicationDAO.findById(id);
	}

	@Override
	public List<ApplicationVO> findAll() {
		return applicationDAO.findAll();
	}

	@Override
	public List<ApplicationBean> findApplicationByPage(String userId,String appName, String providerName,PageInfoBean pageInfoBean) {
		final List<?> applicationBeanList = applicationDAO.findApplicationByPage(userId, appName, providerName, pageInfoBean);
		final List<ApplicationBean> tempApplicationBeanList = new ArrayList<ApplicationBean>();
 
		for (int i = 0; i < applicationBeanList.size(); i++) {
			final ApplicationBean applicationBean = new ApplicationBean();
			Object [] objectArray =(Object[])applicationBeanList.get(i);
		    applicationBean.setApplicationVO((ApplicationVO)objectArray[0]);
		    applicationBean.setOrganizationVO((OrganizationVO) objectArray[1]);
			tempApplicationBeanList.add(applicationBean);
		}
		return tempApplicationBeanList;
	}
	
	@Override
	public List<ApplicationBean> findApplicationByPageByOrganization(final String organizationId, final String applicationTypeKeyId, final PageInfoBean pageInfoBean){
		final List<?> applicationBeanList = applicationDAO.findApplicationByPageByOrganization(organizationId, applicationTypeKeyId, pageInfoBean);
		final List<ApplicationBean> tempApplicationBeanList = new ArrayList<ApplicationBean>();
 
		for (int i = 0; i < applicationBeanList.size(); i++) {
			final ApplicationBean applicationBean = new ApplicationBean();
			final Object[] objectArray =(Object[])applicationBeanList.get(i);
		    applicationBean.setApplicationVO((ApplicationVO)objectArray[0]);
		    applicationBean.setOrganizationVO((OrganizationVO) objectArray[1]);
			tempApplicationBeanList.add(applicationBean);
		}
		return tempApplicationBeanList;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void editApplication(ApplicationVO applicationVO,SessionUserBean sessionUserBean) {
		ApplicationVO tempApplicationVO =findById(applicationVO.getApplicationId());
		
		tempApplicationVO.setApplicationName(applicationVO.getApplicationName());
		tempApplicationVO.setApplicationTypeKeyId(applicationVO.getApplicationTypeKeyId());
		tempApplicationVO.setImplementFlag(applicationVO.getImplementFlag());
		tempApplicationVO.setCommomFlag(applicationVO.getCommomFlag());
		tempApplicationVO.setApplicationVersion(applicationVO.getApplicationVersion());
		tempApplicationVO.setAppUrl(applicationVO.getAppUrl());
		tempApplicationVO.setApplicationDesc(applicationVO.getApplicationDesc());
		tempApplicationVO.setPrice(applicationVO.getPrice());
		tempApplicationVO.setImageUrl(applicationVO.getImageUrl());
		tempApplicationVO.setSupplier(applicationVO.getSupplier());
		tempApplicationVO.setStatus(applicationVO.getStatus());
		tempApplicationVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());//
		tempApplicationVO.setUpdateDate(DateUtil.getNowDate());
		tempApplicationVO.setMemo(applicationVO.getMemo());
		
		applicationDAO.update(tempApplicationVO,Constants.LOGGER_OPERATE_FLAG_UPDATE,sessionUserBean.getUserInfoVO().getUserId());
		
		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void addApplication(ApplicationVO applicationVO,SessionUserBean sessionUserBean) {

		applicationVO.setCreator(sessionUserBean.getUserInfoVO().getUserId());
		applicationVO.setCreateDate(DateUtil.getNowDate());
		applicationVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());
		applicationVO.setUpdateDate(DateUtil.getNowDate());
		applicationDAO.save(applicationVO,sessionUserBean.getUserInfoVO().getUserId());


		
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	public void controlApplication(String applicationId, String status,SessionUserBean sessionUserBean) {
		String operateFlag=Constants.STATE_NORMAL.equals(status) ? Constants.LOGGER_OPERATE_FLAG_START:Constants.LOGGER_OPERATE_FLAG_STOP;
		ApplicationVO tempApplicationVO =findById(applicationId);
		tempApplicationVO.setStatus(status);
		tempApplicationVO.setUpdater(sessionUserBean.getUserInfoVO().getUserId());
		tempApplicationVO.setUpdateDate(DateUtil.getNowDate());
		applicationDAO.update(tempApplicationVO,operateFlag,sessionUserBean.getUserInfoVO().getUserId());
		
		
	}



}
